﻿//-----------------------------------------------------------------
//-------演示作用--------------------------------------------------
//-------mqtt_client_demo--------by.mky----------------------------
//-----------------------------------------------------------------
#include <csignal>
#include <iostream>
#include <string.h>
#include <thread>
#include <unistd.h> 
#include "mqtt/pro_mgr.h"
#include <string.h> // for strlen
//-----------------------------------------------------------------
using namespace std;
bool g_bRun = true;

//-----------------------------------------------------------------
// 将16进制字符串，转成字节数组
//static void hex_str_to_byte(const char* source, unsigned char* dest, int sourceLen)
//{
//	short i;
//	unsigned char highByte, lowByte;
//
//	for (i = 0; i < sourceLen; i += 2)
//	{
//		highByte = toupper(source[i]);
//		lowByte = toupper(source[i + 1]);
//
//		if (highByte > 0x39)
//			highByte -= 0x37;
//		else
//			highByte -= 0x30;
//
//		if (lowByte > 0x39)
//			lowByte -= 0x37;
//		else
//			lowByte -= 0x30;
//
//		dest[i / 2] = (highByte << 4) | lowByte;
//	}
//}

// 将字节数组，转成16进制字符串
static string byte_to_hex_string(const void* buf, size_t len)
{
	string ret;
	char tmp[8];
	const uint8_t* data = (const uint8_t*)buf;

	for (size_t i = 0; i < len; i += 16) {
		for (int j = 0; j < 16; ++j) {
			if (i + j < len) {
				int sz = sprintf(tmp, "%.2x", data[i + j]);
				ret.append(tmp, sz);
			}
		}
	}

	return ret;
}
void cmdThread()
{
	while (true)
	{
		char cmdBuf[256] = {};
		scanf("%s", cmdBuf);
		if (0 == strcmp(cmdBuf, "exit"))
		{
			g_bRun = false;
			printf("退出cmdThread线程\n");
			break;
		}
		else {
			printf("不支持的命令。\n");
		}
	}
}
int char_to_hex(char src) {
	if (src == '0')return 0;
	else if (src == '1')return 1;
	else if (src == '2')return 2;
	else if (src == '3')return 3;
	else if (src == '4')return 4;
	else if (src == '5')return 5;
	else if (src == '6')return 6;
	else if (src == '7')return 7;
	else if (src == '8')return 8;
	else if (src == '9')return 9;
	else if (src == 'A')return 10;
	else if (src == 'B')return 11;
	else if (src == 'C')return 12;
	else if (src == 'D')return 13;
	else if (src == 'E')return 14;
	else if (src == 'F')return 15;
	else return 0;
}
std::string getHex(std::string src) {
	std::string out;
	int test;
	int test2;
	int result;
	bool tag = true;
	for (auto it : src) {
		if (tag) {
			tag = false;
			test = char_to_hex(it);
		}
		else {
			test2 = char_to_hex(it);
			result = test * 16 + test2;
			printf("%d ", result);
			out += (char)('0' + result);
			tag = true;
		}
		//result = char_to_hex(it);
		//printf("%d ", result);
		//out += (char)(result);
	}
	printf("\n");
	return std::move(out);
}
////设备注册
//std::string topic_device_register = "/maxvision/sys/e5939a4957734efa8cc8676749d3e1f3/9401102f461a48759aef37cd8378d15c/maxvision/sys/device/register";
//std::string topic_device_register_reply = "/maxvision/sys/e5939a4957734efa8cc8676749d3e1f3/9401102f461a48759aef37cd8378d15c/device/register_reply";
////属性设置
//std::string topic_property_set = "/maxvision/sys/e5939a4957734efa8cc8676749d3e1f3/9401102f461a48759aef37cd8378d15c/thing/property/set";
//std::string topic_property_set_reply = "/maxvision/sys/e5939a4957734efa8cc8676749d3e1f3/9401102f461a48759aef37cd8378d15c/thing/property/set_reply";
////属性获取
//std::string topic_property_get = "/maxvision/sys/e5939a4957734efa8cc8676749d3e1f3/9401102f461a48759aef37cd8378d15c/thing/property/get";
//std::string topic_property_get_reply = "/maxvision/sys/e5939a4957734efa8cc8676749d3e1f3/9401102f461a48759aef37cd8378d15c/thing/property/get_reply";
////动作
//std::string topic_action_exec = "/maxvision/sys/e5939a4957734efa8cc8676749d3e1f3/9401102f461a48759aef37cd8378d15c/thing/action/{模块id:行为id}/exec";
//std::string topic_action_exec_reply = "/maxvision/sys/e5939a4957734efa8cc8676749d3e1f3/9401102f461a48759aef37cd8378d15c/thing/action/{模块id:行为id}/exec_reply";
////时序事件
//std::string topic_tsevent_post = "/maxvision/sys/e5939a4957734efa8cc8676749d3e1f3/9401102f461a48759aef37cd8378d15c/thing/tsevent/{模块id:行为id}/post";
//std::string topic_tsevent_post_reply = "/maxvision/sys/e5939a4957734efa8cc8676749d3e1f3/9401102f461a48759aef37cd8378d15c/thing/tsevent/{模块id:行为id}/post_reply";
////非时序事件
//std::string topic_event_post = "/maxvision/sys/e5939a4957734efa8cc8676749d3e1f3/9401102f461a48759aef37cd8378d15c/thing/event/+/post";
//std::string topic_event_post_reply = "/maxvision/sys/e5939a4957734efa8cc8676749d3e1f3/9401102f461a48759aef37cd8378d15c/thing/event/+/post_reply";
//-----------------------------------------------------------------
int main() {
	mky::ProMgr::getSingleton()
		->getMqttClient()
		->open("/mnt/e/contrle_version/modle_code/mvMqttServer/conf/secret.ini");
	mky::ProMgr::getSingleton()
		->getMqttClient()
		->connect();

	//退出程序事件处理
	std::thread t1(cmdThread);
	t1.detach();
	//mky::ProMgr::getSingleton()
	//	->getMqttClient()
	//	->subscribe("/maxvision/1/thing/ai/device/config/post", 1);
	//mky::ProMgr::getSingleton()
	//	->getMqttClient()
	//	->subscribe("/maxvision/2/thing/ai/device/config/post", 1);
	while (g_bRun)
	{
		sleep(1);
		printf("空闲时间处理其它业务..\n");
		int rc;
		//rc = mky::ProMgr::getSingleton()
		//	->getMqttClient()
		//	->connect();
		
		//rc = mky::ProMgr::getSingleton()
		//	->getMqttClient()
		//	->publish(
		//		"/maxvision/sys/e5939a4957734efa8cc8676749d3e1f3/9401102f461a48759aef37cd8378d15c/thing/property/set",
		//		"{\"clientId\" : \"1514061113896022016\",\"method\" : \"thing.property.set\",\"params\" :{\"areaName\" : \"lhzx\",\"sign\" : \"sign\",\"timeStamp\" : \"timeStamp\"},\"version\" : \"1.0\"}",
		//		1);
		//rc = mky::ProMgr::getSingleton()
		//	->getMqttClient()
		//	->publish(
		//		"/maxvision/sys/e5939a4957734efa8cc8676749d3e1f3/9401102f461a48759aef37cd8378d15c/thing/action/111:222/exec",
		//		"{\"clientId\" : \"1514061113896022016\",\"method\" : \"thing.action.exec\",\"params\" :{\"areaName\" : \"lhzx\",\"sign\" : \"sign\",\"timeStamp\" : \"timeStamp\"},\"version\" : \"1.0\"}",
		//		1);
		//rc = mky::ProMgr::getSingleton()
		//	->getMqttClient()
		//	->unsubscribe("/maxvision/sys/e5939a4957734efa8cc8676749d3e1f3/9401102f461a48759aef37cd8378d15c/thing/property/set");
		sleep(1);
		//if (mky::ProMgr::getSingleton()->getMqttClient()->keepAlive()) {
		//	rc = mky::ProMgr::getSingleton()
		//		->getMqttClient()->disconnect();
		//	//rc = mky::ProMgr::getSingleton()
		//	//	->getMqttClient()->close();
		//	sleep(2);
		//}

	}

	printf("已退出。\n");
	getchar();
	return 0;
}

//-----------------------------------------------------------------
